package cn.onepiece.smili.render.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import cn.onepiece.smili.render.info.TableInfo;
import cn.onepiece.smili.render.RenderService;
import freemarker.template.Template;

/**
 * @Author: haoting.wang
 * @Date: Created in 下午6:00 2017/5/27
 */
@Service
public class TableRenderServiceImpl implements RenderService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    private static Logger        logger = LoggerFactory.getLogger(TableRenderServiceImpl.class);

    /**
     * 模板渲染，并输出到指定路径(savePath)
     *
     * @param tableInfo 表信息
     * @param savePath 保存路径
     * @return
     */
    public void exportTemplate(TableInfo tableInfo, String savePath, String templageName, String prefix,
                               String suffix) {
        Template template = getTemplate(templageName);

        savePath = mkdirs(savePath, prefix);

        File export = new File(savePath, tableInfo.getTableName() + suffix);
        render(tableInfo, template, export);
    }

    private String mkdirs(String savePath, String prefix) {
        savePath = savePath + "/" + prefix;
        File path = new File(savePath);
        if (!path.exists()) {
            path.mkdirs();
        }
        return savePath;
    }

    @Override
    public void render(TableInfo tableInfo, Template template, File export) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(export);
        } catch (IOException e) {
            logger.error("get template error,e={}", e.getMessage());
            throw new RuntimeException(e);

        }
        try {
            template.process(tableInfo, writer);
        } catch (Exception e) {
            logger.error("template process error,e={}", e.getMessage());
            throw new RuntimeException(e);

        }
    }

    @Override
    public Template getTemplate(String ftl) {
        Template template = null;
        try {
            template = freeMarkerConfigurer.getConfiguration().getTemplate(ftl);
        } catch (IOException e) {
            logger.error("get template error,e={}", e.getMessage());
            throw new RuntimeException(e);
        }
        return template;
    }

    public void exportModel(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/model.ftl");
        savePath = mkdirs(savePath, "/model");

        File export = new File(savePath, tableInfo.getClassName() + "DO.java");
        render(tableInfo, template, export);
    }

    public void exportMapper(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/mapper.ftl");
        savePath = mkdirs(savePath, "/mapper");
        File export = new File(savePath, tableInfo.getClassName() + "Dao.java");
        render(tableInfo, template, export);
    }

    public void exportMapperXml(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/mapperXml.ftl");
        savePath = mkdirs(savePath, "/mapper");
        File export = new File(savePath, tableInfo.getClassName() + "Mapper.xml");
        render(tableInfo, template, export);
    }

    public void exportService(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/service.ftl");
        savePath = mkdirs(savePath, "/service");
        File export = new File(savePath, tableInfo.getClassName() + "Service.java");
        render(tableInfo, template, export);
    }

    public void exportServiceImpl(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/serviceImpl.ftl");
        savePath = mkdirs(savePath, "/service/impl");
        File export = new File(savePath, tableInfo.getClassName() + "ServiceImpl.java");
        render(tableInfo, template, export);
    }

    public void exportController(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/controller.ftl");
        savePath = mkdirs(savePath, "/controller");
        File export = new File(savePath, tableInfo.getClassName() + "Controller.java");
        render(tableInfo, template, export);
    }

    public void exportDto(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("td/searchDTO.ftl");
        savePath = mkdirs(savePath, "/dto");
        File export = new File(savePath, tableInfo.getClassName() + "SearchDTO.java");
        render(tableInfo, template, export);
    }

    @Override
    public void exportHtml(TableInfo tableInfo, String savePath) {
        exportFrame(tableInfo,savePath);
        exportContent(tableInfo,savePath);
        exportForm(tableInfo,savePath);
        exportSearchForm(tableInfo,savePath);
        exportTable(tableInfo,savePath);
    }

    private void exportFrame(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("html/model.ftl");
        savePath = mkdirs(savePath, "/html");
        File export = new File(savePath, tableInfo.getClassName() + ".js");
        render(tableInfo, template, export);
    }

    private void exportContent(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("html/modelContent.ftl");
        savePath = mkdirs(savePath, "/html");
        File export = new File(savePath, tableInfo.getClassName() + "Content.js");
        render(tableInfo, template, export);
    }

    private void exportForm(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("html/modelForm.ftl");
        savePath = mkdirs(savePath, "/html");
        File export = new File(savePath, tableInfo.getClassName() + "Form.js");
        render(tableInfo, template, export);
    }

    private void exportSearchForm(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("html/modelSearchForm.ftl");
        savePath = mkdirs(savePath, "/html");
        File export = new File(savePath, tableInfo.getClassName() + "SearchForm.js");
        render(tableInfo, template, export);
    }

    private void exportTable(TableInfo tableInfo, String savePath) {
        Template template = getTemplate("html/modelTable.ftl");
        savePath = mkdirs(savePath, "/html");
        File export = new File(savePath, tableInfo.getClassName() + "Table.js");
        render(tableInfo, template, export);
    }
}
